package com.amumu.drama.common.utils;

import com.beust.jcommander.internal.Maps;
import org.apache.commons.lang3.StringUtils;

import java.util.*;


/**
 * 属性工具类
 * Created by leen.wx on 3/31 0026.
 */
public class AttributeUtil {

    static final String SP = ";";
    static final String SSP = ":";
    static final String R_SP = "#3A";
    static final String R_SSP = "#3B";

    /**
     * 通过Map转换成String
     *
     * @param attrs
     * @return
     */
    public static String toString(Map<String, String> attrs) {
        StringBuilder sb = new StringBuilder();
        if (null != attrs && !attrs.isEmpty()) {
            sb.append(SP);
            List<String> list = new ArrayList<String>(attrs.keySet());
            Collections.sort(list);
            for (String key : list) {
                String val = attrs.get(key);
                if (StringUtils.isNotBlank(val)) {
                    sb.append(encode(key)).append(SSP).append(encode(val)).append(SP);
                }
            }
        }
        return sb.toString();
    }


    /**
     * 通过字符串解析成attributes
     * @param str
     * @return
     */
    public static Map<String, String> toMap(String str) {
        Map<String, String> attrs = new HashMap<>();
        if (StringUtils.isNotBlank(str)) {
            String[] arr = StringUtils.split(str, SP);
            for (String kv : arr) {
                if (StringUtils.isNotBlank(kv)) {
                    String[] ar = kv.split(SSP);
                    if (ar.length == 2) {
                        String k = decode(ar[0]);
                        String v = decode(ar[1]);
                        if (StringUtils.isNotBlank(k) && StringUtils.isNotBlank(v)) {
                            attrs.put(k, v);
                        }
                    }
                }
            }
        }
        return attrs;
    }

    /**
     * 通过字符串解析成attributes并插入kv
     * @param str 源字符串
     * @param key 插入的key
     * @param val 插入的val
     * @return 插入kv之后的map
     */
    public static Map<String, String> toMapAndPut(String str, String key, String val) {
        Map<String, String> attrs = toMap(str);
        attrs.put(key, val);
        return attrs;
    }

    /**
     * 解析字符串成attributes并获取key的value
     * @param str
     * @param key
     * @return
     */
    public static String toMapAndGet(String str, String key) {
        return toMap(str).get(key);
    }

    public static boolean containsKey(String str, String key, String expectValue) {
        Map<String, String> attrMap = toMap(str);
        if (StringUtils.isBlank(expectValue)) {
            return attrMap.containsKey(key);
        } else {
            return Objects.equals(attrMap.get(key), expectValue);
        }
    }

    public static String encode(String val) {
        return StringUtils.replace(StringUtils.replace(val, SP, R_SP), SSP, R_SSP);
    }

    public static String decode(String val) {
        return StringUtils.replace(StringUtils.replace(val, R_SP, SP), R_SSP, SSP);
    }

}
